package game;

import util.StringUtil;

public class StitcherStatus {
	private static StitcherStatus instance;
	public static StitcherStatus get(){
		if(instance==null){
			instance = new StitcherStatus();
		}
		return instance;
	}
	
	public boolean closeRequestedYN = false;
	public boolean inputFileChangedYN = false;
	public boolean outputFileSaveYN = false;
	
	//input file text
	public String inputFileText = "";
	
	public String[][] getTextureNames(){
		String lines[] = inputFileText.split("\\r?\\n");
		String[][] matrix = new String[lines.length][];
		String delims = "[,]";
		
		for(int i=0;i <lines.length; i++){
			String line = lines[i];
			matrix[i] =line.split(delims);
			for (int j=0;j<matrix[i].length;j++){
				matrix[i][j] =matrix[i][j].trim();
			}
		}		
		return matrix;
	}
	
	private int getMaxLengthOfTextureName(){
		int maxSize=0;
		String[][] matrix = getTextureNames();
		for(int i=0;i<matrix.length;i++){
			String[] line = matrix[i];
			for (int j=0;j<line.length;j++){
				if(line[j]==null){
					continue;
				}
				maxSize  = Math.max(maxSize, line[j].trim().length());
			}
		}
		return maxSize;
	}
	
	private static String padRight(String s, int n) {
	     return String.format("%1$-" + n + "s", s);  
	}
	
	public String getPaddedTextureName(){
		String lineSeparator = System.getProperty("line.separator");
		int maxSize=getMaxLengthOfTextureName();
		String[][] oldMatrix = getTextureNames();
		String[][] newMatrix = new String[oldMatrix.length][];
		for(int i=0;i<oldMatrix.length;i++){
			String[] oldLine = oldMatrix[i];
			String[] newLine = new String[oldLine.length];
			newMatrix[i] =newLine;
			for (int j=0;j<oldLine.length;j++){
				String name =oldLine[j].trim();
				newLine[j] = padRight(name,maxSize);
			}
		}
		
		StringBuffer sb = new StringBuffer(1024);
		for(int i=0;i<newMatrix.length;i++){
			String[] newLine = newMatrix[i];
			if (i > 0)
				sb.append(lineSeparator);
			sb.append(StringUtil.join(newLine, ","));
		}
		return sb.toString();
	}
}